package org.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.zhxy.model.entity.Admin;
import org.zhxy.model.entity.Teacher;
import org.zhxy.service.TeacherService;
import org.zhxy.mapper.TeacherMapper;
import org.springframework.stereotype.Service;

/**
 * @author walter
 * @description 针对表【tb_teacher】的数据库操作Service实现
 * @createDate 2025-07-01 14:17:11
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Teacher findByName(String name) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getName, name);
        return getOne(wrapper);
    }

    // 分页条件查询教师列表
    @Override
    public IPage<Teacher> page(IPage page, Teacher teacher) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        String clazzName = teacher.getClazzName();
        wrapper.eq(StringUtils.hasText(clazzName), Teacher::getClazzName, clazzName);
        String name = teacher.getName();
        wrapper.like(StringUtils.hasText(name), Teacher::getName, name);
        wrapper.orderByDesc(Teacher::getId);
        return page(page, wrapper);
    }

    @Override
    public boolean updatePwd(Integer id, String newPwd) {
        LambdaUpdateWrapper<Teacher> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Teacher::getId, id);
        wrapper.set(Teacher::getPassword, DigestUtils.md5DigestAsHex(newPwd.getBytes()));
        return update(wrapper);
    }
}




